<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="流程名称">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入流程名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="createPublish"
          >新建流程发布</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="urlList">
      <!-- <el-table-column label="编号" prop="id" width="120" /> -->
      <el-table-column
        label="流程名称"
        prop="processName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="分享链接" align="center" prop="shareUrl">
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="登录验证"
        align="center"
        prop="loginType"
        width="180"
      >
        <template v-slot="scope">
          <span>{{ scope.row.loginType == 0 ? "不需要" : "需要" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            class="copy_url"
            @click="handleUpdate(scope.row)"
            :data-clipboard-text="scope.row.shareUrl"
            >复制</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新建发布对话框 -->
    <el-dialog
      title=""
      :visible.sync="openPublishDialog"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <publishProcess ref="publishForm" />
    </el-dialog>
  </div>
</template>

<script>
import { getProcessUrlList, deleteProcessUrl } from "@/api/bpm/definition";

import publishProcess from "./publish.vue";
import Clipboard from "clipboard";

export default {
  name: "Publish",
  components: { publishProcess },
  data() {
    return {
      //新建流程发布
      openPublishDialog: false,
      // 总条数
      total: 0,
      // 发布url表格数据
      urlList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        processName: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    closeDialog() {
      this.$refs["publishForm"].resetPublishForm();
      this.getList();
    },
    /** 查询流程发布列表 */
    getList() {
      this.loading = true;
      getProcessUrlList(this.queryParams).then((response) => {
        this.urlList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /**新建流程发布 */
    createPublish() {
      this.openPublishDialog = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate() {
      var clipboard = new Clipboard(".copy_url");
      clipboard.on("success", (e) => {
        this.$message.success("复制成功");
        // 释放内存
        clipboard.destroy();
      });
      clipboard.on("error", (e) => {
        // 不支持复制
        this.$message.error("该浏览器不支持自动复制");
        // 释放内存
        clipboard.destroy();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      deleteProcessUrl(id).then((res) => {
        this.getList();
        this.$message.success("删除成功！");
      });
    },
  },
};
</script>
